/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * loginJInternalFrame.java
 *
 * Created on 24/10/2009, 12:13:31
 */
package br.com.fatecpg.zanotti.jframes;

import br.com.fatecpg.zanotti.modelo.Sessao;
import br.com.fatecpg.zanotti.acessobanco.conFunc;
import br.com.fatecpg.zanotti.jframes.consultas.conProdutoJInternalFrame;
import br.com.fatecpg.zanotti.modelo.Funcionario;

/**
 *
 * @author Luiz Zanotti
 */
public class loginJInternalFrame extends javax.swing.JInternalFrame {

    /** Creates new form loginJInternalFrame */
    public loginJInternalFrame() {
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        loginjPanel = new javax.swing.JPanel();
        titulojLabel = new javax.swing.JLabel();
        senhajPasswordField = new javax.swing.JPasswordField();
        loginjTextField = new javax.swing.JTextField();
        loginjLabel = new javax.swing.JLabel();
        senhajLabel = new javax.swing.JLabel();
        confirmajButton = new javax.swing.JButton();

        setClosable(true);
        setDefaultCloseOperation(javax.swing.WindowConstants.HIDE_ON_CLOSE);
        setTitle("Login");

        loginjPanel.setBorder(javax.swing.BorderFactory.createTitledBorder(""));

        titulojLabel.setFont(new java.awt.Font("Tahoma", 0, 48)); // NOI18N
        titulojLabel.setForeground(new java.awt.Color(0, 102, 255));
        titulojLabel.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        titulojLabel.setText("Thata Soft");

        senhajPasswordField.setText("jField1");
        senhajPasswordField.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                senhajPasswordFieldFocusGained(evt);
            }
        });

        loginjLabel.setText("Login");

        senhajLabel.setText("Senha");

        confirmajButton.setText("Confirma");
        confirmajButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                confirmajButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout loginjPanelLayout = new javax.swing.GroupLayout(loginjPanel);
        loginjPanel.setLayout(loginjPanelLayout);
        loginjPanelLayout.setHorizontalGroup(
            loginjPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, loginjPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(loginjPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(titulojLabel, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 235, Short.MAX_VALUE)
                    .addGroup(loginjPanelLayout.createSequentialGroup()
                        .addGroup(loginjPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(loginjPanelLayout.createSequentialGroup()
                                .addGap(54, 54, 54)
                                .addComponent(loginjLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(loginjPanelLayout.createSequentialGroup()
                                .addGap(49, 49, 49)
                                .addComponent(senhajLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(loginjPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(loginjTextField)
                            .addComponent(senhajPasswordField, javax.swing.GroupLayout.DEFAULT_SIZE, 59, Short.MAX_VALUE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(confirmajButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                .addContainerGap())
        );
        loginjPanelLayout.setVerticalGroup(
            loginjPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(loginjPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(titulojLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 46, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(loginjPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(loginjTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(loginjLabel))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(loginjPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(senhajLabel)
                    .addComponent(senhajPasswordField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(confirmajButton))
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(loginjPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(loginjPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void confirmajButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_confirmajButtonActionPerformed
        String cdfunc = loginjTextField.getText();
        String cdsenha = String.valueOf(senhajPasswordField.getPassword());

        if (br.com.fatecpg.zanotti.acessobanco.oralogin.logar(cdfunc, cdsenha) == true) {
            Funcionario Func = new Funcionario();
            Func = conFunc.consultar(cdfunc);
MDI.getInstancia().setLogarjMenuItem(false);
            Sessao.getInstance().setCDCARGO(Func.getCDCARGO());
            Sessao.getInstance().setCDFUNCIONARIO(Func.getCDFUNCIONARIO());
            Sessao.getInstance().setCDLOJA(Func.getCDLOJA());
            Sessao.getInstance().setNMCARGO(Func.getNMCARGO());
            Sessao.getInstance().setNMFUNCIONARIO(Func.getNMFUNCIONARIO());
            MDI.getInstancia().setDADOSDOUSUARIOjLabel("Usuário logado: " + Func.getNMFUNCIONARIO() + " - Cargo:" + Func.getNMCARGO());

            int cdcargo = libera(cdfunc, cdsenha);

            if (cdcargo != 0) {
                javax.swing.JOptionPane.showMessageDialog(null, "Você logou com sucesso " + Sessao.getInstance().getNMFUNCIONARIO());
                this.setVisible(false);
                switch (cdcargo) {
                    case 1:
                        MDI.getInstancia().liberaTudo(true);
                        MDI.getInstancia().libera1(true);
                        MDI.getInstancia().libera2(true);
                        break;
                    case 2:
                        MDI.getInstancia().liberaTudo(true);
                        MDI.getInstancia().libera2(true);
                        break;
                    case 3:
                        MDI.getInstancia().liberaVenda(true);
                        MDI.getInstancia().liberaConProduto(true);
                        MDI.getInstancia().libera3(true);

                        break;
                    case 4:
                        MDI.getInstancia().liberaConProduto(true);
                        MDI.getInstancia().libera4(true);
                        
                        break;
                }
            }


        } else {
            javax.swing.JOptionPane.showMessageDialog(null, "Senha e/ou login incorretos.", "Atenção", 2);
            reseta();
        }
}//GEN-LAST:event_confirmajButtonActionPerformed

    public void reseta() {
senhajPasswordField.setText("");
loginjTextField.setText("");
MDI.getInstancia().setLogarjMenuItem(true);
    }
    private void senhajPasswordFieldFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_senhajPasswordFieldFocusGained
        senhajPasswordField.setText("");
    }//GEN-LAST:event_senhajPasswordFieldFocusGained
    private static loginJInternalFrame p;

    public static loginJInternalFrame getInstancia() {
        if (p == null) {
            p = new loginJInternalFrame();
        }
        return p;
    }

    public int libera(String cdfunc, String cdsenha) {
        return br.com.fatecpg.zanotti.acessobanco.oraCargo.consultar(cdfunc, cdsenha);
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton confirmajButton;
    private javax.swing.JLabel loginjLabel;
    private javax.swing.JPanel loginjPanel;
    private javax.swing.JTextField loginjTextField;
    private javax.swing.JLabel senhajLabel;
    private javax.swing.JPasswordField senhajPasswordField;
    private javax.swing.JLabel titulojLabel;
    // End of variables declaration//GEN-END:variables
}
